﻿@{
    ViewData["Title"] = "设备类型设置";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>设备类型设置</title>
    <link href="//unpkg.com/layui@2.9.17/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <form class="layui-form layui-row">
        <div class="layui-col-md4">
            <div class="layui-input-wrap">
                <label class="layui-form-label">类型名称：</label>
                <input type="text" style="width:180px" id="name" value="" placeholder="请输入分类名称" class="layui-input" lay-affix="clear">
            </div>
        </div>
        <div class="layui-col-md4">
            <div class="layui-input-wrap">
                <label class="layui-form-label">是否启用： </label>
                <div class="layui-input-block" style="width:180px">
                    <select id="type" lay-filter="aihao">
                        <option value="" selected></option>
                        <option value="Y">是</option>
                        <option value="F">否</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-btn-container ">
            <button class="layui-btn" lay-submit lay-filter="demo-table-search">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary">清空</button>
        </div>
    </form>
    <table class="layui-hide" id="ID-treeTable-demo"></table>
    <script type="text/html" id="TPL-treeTable-demo-tools">
        <div class="layui-btn-container">
          <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">编辑</a>
          <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="addChild">新增</a>
          <a class="layui-btn layui-btn-xs" lay-event="delChild">删除</a>
        </div>
    </script>
    <script src="~/Scripts/jquery-3.4.1.min.js"></script>
    <script src="//unpkg.com/layui@2.9.17/dist/layui.js"></script>
    <script>
        layui.use(function () {
            var treeTable = layui.treeTable;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            //显示
            function show() {
                treeTable.render({
                    elem: '#ID-treeTable-demo',
                    url: '/Li/GetDVMachineryType',
                    tree: {
                        customName: {
                            name: 'machineTypeCode',
                        },
                        view: {
                            expandAllDefault: true,
                        },
                    },
                    page: false,
                    //maxHeight: '530px',
                    //toolbar: '#TPL-treeTable-demo',
                    cols: [[
                        { field: 'machineTypeCode', title: '设备类型编码', fixed: 'left' },
                        { field: 'machineTypeName', title: '设备类型名称' },
                        { field: 'remark', title: '备注' },
                        {
                            field: 'enableFlag', title: '是否启用', templet: function (row) {
                                var tmp = row.enableFlag == 'Y' ? "是" : "否";
                                return tmp;
                            }
                        },
                        
                        {
                            fixed: "right", title: "操作", align: "center", templet: function (row) {
                                var tmp = '<a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="detail">修改</a>';

                                tmp += '<a class="layui-btn layui-btn-xs" lay-event="addChild">新增</a>';

                                if (row.id > 1) {
                                    tmp += '<a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delChild">删除</a>';
                                }
                                return tmp;
                            }
                        }
                    ]],

                });
            }

            show();//初始化显示
            // 搜索提交
            form.on('submit(demo-table-search)', function (data) {
                var machineTypeName = $("#name").val();    //分类名称
                var enableFlag = $("#type").val();    //是否启用
                // 执行搜索重载
                table.reload('ID-treeTable-demo', {
                    // page: {
                    //     curr: 1 // 重新从第 1 页开始
                    // },
                    where: {
                        machineTypeName: machineTypeName,
                        enableFlag: enableFlag
                    }// 搜索的字段
                });
                return false; // 阻止默认 form 跳转
            });

            // 单元格工具事件
            treeTable.on('tool(ID-treeTable-demo)', function (obj) {
                var layEvent = obj.event; // 获得 lay-event 对应的值
                var trData = obj.data;
                if (layEvent === "detail") {
                    var data = obj.data;//获取表单中的点击按钮的那一行数据
                    console.log(data);
                    machineTypeCode = data.machineTypeCode;
                    machineTypeName = data.machineTypeName;
                    enableFlag = data.enableFlag;
                    remark = data.remark;
                    id = data.id;
                    createTime = data.createTime;
                    createBy = data.createBy;
                    layer.open({
                        title: '修改分类',
                        skin: 'layui-layer-rim', //加上边框
                        type: 2,
                        shadeClose: true, // 点击遮罩区域，关闭弹层
                        area: ['50%', '50%'],
                        content: '/Li/DVMachineryTypeUpdate',
                        end: function () {
                            show();
                        }
                    });
                } else if (layEvent === "addChild") {
                    sessionStorage.setItem("ItemTypeName", trData.itemTypeName);
                    sessionStorage.setItem("ParentTypeId", trData.id);
                    layer.open({
                        type: 2,
                        title: '添加分类',
                        shadeClose: true, // 点击遮罩区域，关闭弹层
                        skin: 'layui-layer-rim', //加上边框
                        area: ['620px', '440px'], //宽高
                        content: '/Song/AddMdItemType',
                        end: function () {
                            show();//刷新父级页面列表方法，类似于回调函数
                        }
                    });
                } else if (layEvent === "delChild") {
                    layer.confirm("确定要删除吗？", function (data) {
                        var id = { id: trData.id };
                        // 此处可执行 Ajax 等操作
                        $.get('/Song/DeleteMdItemTypeAsyncs', id, function (res) {
                            if (res) {
                                layer.msg("删除成功", { icon: 1, time: 1000 }, function () {
                                    obj.del();
                                    // 刷新表格
                                    table.reload('ID-treeTable-demo');
                                });
                            } else {
                                layer.msg("删除失败", { icon: 2, time: 1000 });
                            }
                        });
                    });
                }
            });
        });
    </script>
</body>
</html>